package client.comm.baoding.ui

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.GylAdapter
import client.comm.baoding.adapter.main.GylHeaderAdapter
import client.comm.baoding.databinding.ActivityGylGoodslistBinding
import client.comm.baoding.ui.vm.GylViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.comm_ui.draw.SpaceItemBottomWhiteDecoration
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.widget.EmptyLayout
import com.google.gson.Gson
import kotlinx.coroutines.launch

class GylGoodsListActivity : BaseMiddleActivity<GylViewModel, ActivityGylGoodslistBinding>() {

    var cate_id: String = ""

    override fun getLayout(): Int {
        return R.layout.activity_gyl_goodslist
    }

    override fun initViewModel(): GylViewModel = createViewModel()

    val adapter: GylAdapter by lazy {
        GylAdapter(this, mImgLoader).apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA

                } else {
                    binding.emptyLayout.visibility = View.GONE
                }

            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }
        }
    }

    val hengDecoration by lazy {
        SpaceItemBottomWhiteDecoration(10)
    }

    val footer by lazy {
        PagerFooterAdapter()
    }

    val headerAdapter: GylHeaderAdapter by lazy {
        GylHeaderAdapter(mImgLoader, this).apply {

        }
    }

    override fun initView() {
        intent.getStringExtra("title_name")?.let {
            binding.titleName = it
        }
        intent.getStringExtra("cate_id")?.let {
            cate_id = it
        }
        binding.event = this

        binding.recyclerViewList.apply {
            adapter = this@GylGoodsListActivity.adapter.withLoadStateHeaderAndFooter(
                headerAdapter,
                footer
            )

            layoutManager = LinearLayoutManager(context)
            addItemDecoration(hengDecoration)
        }

        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }

    }

    override fun initData() {
        getData()

        viewModel.tradeCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    headerAdapter.goodsIndex = it.data
                    headerAdapter.notifyDataSetChanged()
                } else {
//                    if (adapter.itemCount == 0) {
//                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
//                    }
                }
            }
        })
    }

    fun getData() {
        lifecycleScope.launch {
            viewModel.pagingDataAuto(20, hold = {
                if (it == 1) {
                    var ret = viewModel.server.goods_sku_list(it, "1").body()

                    val g = Gson()

                    val bean = viewModel.tradeCall.value
                    if (bean == null || !g.toJson(bean.data?.category_list)
                            .equals(g.toJson(ret?.data?.category_list))
                    ) {
                        viewModel.tradeCall.postValue(ret)
//                    withContext(Dispatchers.Main) {
//                        viewModel.tradeCall.value = ret
//                    }
                    }

                    PagingJsonResult(
                        list = ret?.data?.goods_list,
                        code = ret?.code,
                        message = ret?.message
                    )
                } else {
                    PagingJsonResult(
                        list = arrayListOf(),
                        code = 200,
                        message = ""
                    )
                }
            }).collect {
                adapter.submitData(it)
            }
        }
    }

}

